"""
获取问财选股数据:筹码集中,股东人数减少10%以上，连续放量,boll突破上轨，
"""
from datetime import datetime
import time

import pandas as pd
from DrissionPage import ChromiumPage
from DrissionPage import WebPage
from DrissionPage._units.listener import DataPacket
from DrissionPage.errors import ElementNotFoundError

from x01_stock.xx_util.DBUtil import SqlHelper

page = ChromiumPage()
page.listen.start('https://www.iwencai.com/customized/chart/get-robot-data')

page.get('https://www.iwencai.com/unifiedwap/result?w=%E7%AD%B9%E7%A0%81%E9%9B%86%E4%B8%AD,%E8%82%A1%E4%B8%9C%E4%BA%BA%E6%95%B0%E5%87%8F%E5%B0%9110%25%E4%BB%A5%E4%B8%8A%EF%BC%8C%E8%BF%9E%E7%BB%AD%E6%94%BE%E9%87%8F,boll%E7%AA%81%E7%A0%B4%E4%B8%8A%E8%BD%A8%EF%BC%8C&querytype=stock')
time.sleep(3)
try: # header = res.request.postData
    stat_date = datetime.now().date().strftime('%Y-%m-%d')
    res = page.listen.wait()  # 等待并获取一个数据包
    print(res.response.body)
    # data1 = res.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    #
    # # 插入问财返回的基本信息
    # for data in data1:
    #     print(data)
    #     sql = (
    #         f"INSERT INTO `z_sproot_series`.`stock_first_zhangting_wencai` (trade_date,jizhongdu90pct,stock_price,jishuxingtai,chengbenxiaxiang90pct,chg_pct,lianxuzhangtingtianshu,mairuxinhao,stock_code ,"
    #         f"shoupanhuoli,aguliutongshizhi,chengbenshangxian90pct,pingjunchengben,jzd90_paiming,stock_name,fangliang,jizhongdu70pct) values ('{stat_date}',")
    #     for key, value in data.items():
    #         print(key + ':' + str(value))
    #         if key != 'market_code' and key != 'code':
    #             print(type(value))
    #             if type(value) == str:
    #                 sql = sql + '"' + str(value) + '",'
    #             else:
    #                 sql = sql + str(value) + ','
    #     sql = sql[:-1]+');'
    #     print('--------------------------------')
    #     print(sql)
    #     SqlHelper.get_instance().execute_curd(sql)
    #
    # # 【第二个查询】
    # print('点击获取表现的数据')
    # page.ele('@text()=表现').click()
    # time.sleep(3)
    # res2 = page.listen.wait()
    # data2 = res2.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    # # print(data2)
    # for data in data2:
    #     print(data)
    #     stock_code = ''
    #     sql = ('update stock_first_zhangting_wencai set jin5rizhangfu = %s , nianchuzhijinzhangfu = %s ,jin3rizhangfu = %s ,'
    #            'jin20rizhangfu = %s ,jin3yuezhangfu = %s ,jin1nianzhangfu = %s ,jin1yuerizhangfu = %s,jin10rizhangfu = %s  ')
    #     set_value = []
    #     for key in data.keys():
    #         if key == 'code' or key == 'market_code' or key == '最新价' or key == '最新涨跌幅' or key == '股票简称':
    #             continue
    #         elif key == '股票代码':
    #             stock_code = data.get(key)
    #         else:
    #             if type(data[key]) == float:
    #                 set_value.append(key + ':' + str(round(data.get(key), 2)))
    #             else:
    #                 set_value.append(key + ':' + data.get(key))
    #     print(set_value)
    #     sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
    #     print(sql)
    #     SqlHelper.get_instance().execute_curd(sql, set_value)
    #
    # # 【 获取技术的数据】
    # page.ele('@text()=技术').click()
    # time.sleep(3)
    # res3 = page.listen.wait()
    # data3 = res3.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    # print(data3)
    #
    # for data in data3:
    #     print(data)
    #     stock_code = ''
    #     sql = ('update stock_first_zhangting_wencai set suoshugainian = %s, zhulizijin = %s , huanshoulv = %s ,ddesanhu = %s ,'
    #            'jingjiajin_e = %s ,geguredu = %s ,jingjialiang = %s ')
    #     set_value = []
    #     for key in data.keys():
    #         if key == 'code' or key == 'market_code' or key == '最新价' or key == '最新涨跌幅' or key == '股票简称' or key.startswith('技术形态') or key.startswith('集中度90'):
    #             continue
    #         elif key == '股票代码':
    #             stock_code = data.get(key)
    #         else:
    #             set_value.append(data.get(key))
    #     print(set_value)
    #     sql = sql + ' where stock_code = "' + stock_code + '" and trade_date = "' + stat_date + '";'
    #     print(sql)
    #     SqlHelper.get_instance().execute_curd(sql, set_value)
except ElementNotFoundError:
    print('程序异常退出')
# page.quit()
# 打印响应的JSON数据[0]
